package cn.edu.cqupt.controller;

import cn.edu.cqupt.exception.ThrowingException;
import cn.edu.cqupt.po.CheckInInfo;
import cn.edu.cqupt.po.ForRoom;
import cn.edu.cqupt.po.Owner;
import cn.edu.cqupt.po.RelatedFile;
import cn.edu.cqupt.po.Room;
import cn.edu.cqupt.po.RoomInfoList;
import cn.edu.cqupt.service.OwnerService;
import cn.edu.cqupt.until.ListResult;
import cn.edu.cqupt.until.PageResult;
import cn.edu.cqupt.until.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Andrew
 * @date 2018/5/25
 */
@Controller
public class OwnerController {
    @Autowired
    OwnerService ownerService;

    @RequestMapping(value = "check",produces = "application/json; charset=UTF-8",method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> checkIn(@RequestBody CheckInInfo checkInInfo){
        boolean success = ownerService.checkIn(checkInInfo);
        return Result.create(success);
    }
    @RequestMapping(value = "room",produces = "application/json; charset=UTF-8",method = RequestMethod.GET)
    @ResponseBody
    public Result<Object> room(Integer roomId){
        Room room = ownerService.getRoom(roomId);
        return Result.success(room);
    }
    @RequestMapping(value = "room/save",produces = "application/json; charset=UTF-8",method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> saveRoom(@RequestBody Room room){
        boolean success = ownerService.saveRoom(room);
        return Result.create(success);
    }
    @RequestMapping(value = "room/list",produces = "application/json; charset=UTF-8",method = RequestMethod.POST)
    @ResponseBody
    public String listRoom(@RequestBody ForRoom forRoom){

        if (forRoom.getPageNum() == null || forRoom.getPageSize() == null)
            throw new ThrowingException("请传入分页参数");
        Page<RoomInfoList> page = PageHelper.startPage(forRoom.getPageNum(),forRoom.getPageSize());
        List<RoomInfoList> roomInfoLists = ownerService.getAllInfo(forRoom);
        return PageResult.success(roomInfoLists)
                .pagesNum(page.getPageNum())
                .pages(page.getPages())
                .total(page.getTotal())
                .toJSONString();
    }
    @RequestMapping(value = "room/file",produces = "application/json; charset=UTF-8",method = RequestMethod.GET)
    @ResponseBody
    public String roomFile(Integer roomId){
        List<RelatedFile> relatedFiles = ownerService.getRoomFiles(roomId);
        return ListResult.success(relatedFiles).toJSONString();
    }
    @RequestMapping(value = "owner",produces = "application/json; charset=UTF-8",method = RequestMethod.GET)
    @ResponseBody
    public String owner(Integer ownerId){
        Owner room = ownerService.getOwner(ownerId);
        return Result.success(room).toJSONString();
    }
    @RequestMapping(value = "owner/save",produces = "application/json; charset=UTF-8",method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> saveOwner(@RequestBody Owner owner){
        boolean success = ownerService.saveOwner(owner);
        return Result.create(success);
    }
}
